using System;
using System.Collections.Generic;
using System.IO;
using SCore.Util;
using UnityEngine;
using CustomSystemLanguage = LocalizationUtils.LocalizationManager.CustomSystemLanguage;

namespace LocalizationUtils.Language
{
    // 备注：多语言里不能包含 ||，程序通过 || 分割行
    public partial class Lang : Singleton<Lang>
    {
        Dictionary<long, string> _langDict = new Dictionary<long, string>();

        // use like:
        // var a = Lang.Get(888);
        public static string Get(long id)
        {
            return Lang.Instance.GetLang(id);
        }

        public static string GetFormat(long id, params object[] args)
        {
            return Lang.Instance.GetFormatLang(id, args);
        }

        public static CustomSystemLanguage GetCurLang()
        {
            return Lang.Instance.GetLang();
        }

        private CustomSystemLanguage _curLang;

        public void SetNative(CustomSystemLanguage nativeLang)
        {
            var old = _curLang;
            _curLang = nativeLang;
            Debug.Log("font  old = " + _curLang + " new = " + nativeLang);
            if (old != nativeLang)
            {
                Load(LocalizationManager.Language2Locale(_curLang));
                //EventManager.BroadCast(GameEventType.ChangeLanguage, old, nativeLang);
            }
        }

        // 不用模版实现，无需过多扩展
        // 确保Language1.xlsm, Language2.xlsm 表结构一致
        private string GetLang(long id)
        {
            if (_langDict.TryGetValue(id, out var s))
            {
                return s;
            }

            Debug.LogError($"Not found lang id {id}");
            return $"{id}-{_curLang}";
        }

        private string GetFormatLang(long id, params object[] args)
        {
            var s = GetLang(id);
            return string.Format(s, args);
        }

        private CustomSystemLanguage GetLang()
        {
            // Debug.LogFormat("Lang GetLang —————————————_curLang = {0}", _curLang);
            return _curLang;
        }

        public void Load(string languageName)
        {
            _langDict.Clear();

            // localization_en_us.csv
            var assetName = $"localization_{languageName}.csv";
            try
            {
                TextAsset text = new TextAsset(""); // var text = AssetManager.LoadAsset<TextAsset>(assetName);

                using (StringReader sr = new StringReader(text.text))
                {
                    string content = sr.ReadToEnd(); // 读取整个文件
                    string[] lines = content.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);

                    foreach (var line in lines)
                    {
                        string[] values = line.Split(',', 2); // 使用逗号作为列分隔符
                        if (values.Length < 2)
                        {
                            Debug.LogError($"Load lang {languageName} failed, line {line} format error");
                            continue;
                        }

                        _langDict.Add(long.Parse(values[0]), values[1]);
                    }
                }
            }
            catch (Exception e)
            {
                Debug.LogError($"Load lang {languageName} failed {e}");
            }

            // debugPrint();
        }
    }
}